/* 
 * Copyright 2010 Joonas Lehtinen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fi.jole.xmlui.editor;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;

import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

import fi.jole.xmlui.XMLUI;
import fi.jole.xmlui.XMLUIException;

@SuppressWarnings("serial")
public class XMLUIEditorApplication extends Application implements
        ClickListener {
    TextField editor = new TextField();
    SplitPanel splitter = new SplitPanel(SplitPanel.ORIENTATION_HORIZONTAL);

    @Override
    public void init() {
        VerticalLayout left = new VerticalLayout();
        Window main = new Window("XMLUI Editor", splitter);
        setMainWindow(main);
        splitter.addComponent(left);
        editor.setSizeFull();
        left.setSizeFull();
        left.addComponent(editor);
        Button testButton = new Button("Test XMLUI", this);
        left.addComponent(testButton);
        left.setExpandRatio(editor, 1.0f);

        fillEditorWithDefaultXML();
    }

    private void fillEditorWithDefaultXML() {
        try {
            URL startingPoint = getClass().getResource("startingPoint.xml");
            if (startingPoint != null) {
                InputStreamReader isr = new InputStreamReader(startingPoint
                        .openStream());
                StringBuffer sb = new StringBuffer();
                char[] buf = new char[1024 * 8];
                int len;
                while ((len = isr.read(buf)) > 0) {
                    sb.append(buf, 0, len);
                }
                isr.close();
                editor.setValue(sb.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void buttonClick(ClickEvent event) {
        try {
            XMLUI xmlui = new XMLUI();
            xmlui.readFrom(new ByteArrayInputStream(editor.toString()
                    .getBytes()));
            splitter.setSecondComponent(xmlui.getRoot());
        } catch (XMLUIException e) {
            VerticalLayout vl = new VerticalLayout();
            vl.setMargin(true);
            vl.addComponent(new Label("<h1>Error</h1>", Label.CONTENT_XHTML));
            StringWriter buf = new StringWriter();
            e.printStackTrace(new PrintWriter(buf));
            vl.addComponent(new Label(buf.toString(),
                    Label.CONTENT_PREFORMATTED));
            splitter.setSecondComponent(vl);
        }
    }

}
